/*
	*在本地使用数据库；
	*第三方库react-native-sqlite-storag的使用； 
*/
import React, {Component} from "react";
import {
	View,
	Text,
	Button,
	TextInput,
} from "react-native";
import SQLite from "react-native-sqlite-storage";


let arr = [];
let listBlock = null;
export default class ShowData extends Component {
	constructor(props) {
		super(props);
		this.state = {
			info: null,
			name: "",
			age: "",
		}
	}

	//增加数据；
	handleDB() {
		let db = SQLite.openDatabase({name: "test.db", location: "default"});
		db.transaction((tx) => {
			tx.executeSql('CREATE TABLE IF NOT EXISTS PersonInfo (name, age, sex, address)');
			tx.executeSql('INSERT INTO PersonInfo VALUES (?,?,?,?)', ['fangming', 25, '男', '宝安区']);
			tx.executeSql('INSERT INTO PersonInfo VALUES (?,?,?,?)', ['zhangsan', 21, '女', '福田区']);
			tx.executeSql('INSERT INTO PersonInfo VALUES (?,?,?,?)', ['lisi', 22, '男', '宝安区']);
			tx.executeSql('INSERT INTO PersonInfo VALUES (?,?,?,?)', ['wangwu', 28, '男', '宝安区']);

		}, (error) => {
			alert("transaction ERROR:" + error.message);
		}, () => {
			alert("success");
		})

	}

	//获取数据；
	handlePress = () => {
		let db = SQLite.openDatabase({name: "test.db", location: "default"});
		db.transaction((tx) => {
			tx.executeSql('SELECT * FROM PersonInfo', [], (tx, results) => {
				console.log("Query completed");
				var len = results.rows.length;
				for(let i = 0; i < len; i ++) {
					let row = results.rows.item(i);
					alert(row);
				}
			});
		})
	}

	//删除数据；
	handleDelete = (text) => {
		let db = SQLite.openDatabase({name: "test.db", location: "default"});
		db.executeSql('DELETE FROM PersonInfo WHERE name = ?', [text], (tx, res) => {
			alert("DELETE success");
		})
	}
	
	//修改数据；
	handleUpdate = (name, age) => {
		let db = SQLite.openDatabase({name: "test.db", location: "default"});
		age = parseInt(age);
		db.executeSql('UPDATE PersonInfo SET age = ? WHERE name = ?', [age, name], (tx, res) => {
			alert("UPDATE success");
		})
	}

	//删除数据库；
	handleDeleteTable() {
		SQLite.deleteDatabase({name: "test.db", location: "defalult"});
	}

	//关闭数据库；
	closeDB = () => {
		let db = SQLite.openDatabase({name: "test.db", location: "default"});
		db.close(() => {
			console.log("DB closed");
		}, (error) => {
			alert("数据库关闭时出现错误" + error.message);
		})
	}

	render() {
		return (
			<View>
				<Text>从数据库中拿数据：</Text>
				<Button 
					title="创建数据"
					onPress={this.handleDB}
				/>
				<View style={{height: 20}}></View>
				<Button 
					title="查询数据"
					onPress={this.handlePress}
				/>
				<View style={{height: 20}}></View>
				<TextInput 
					placeholder="请输入姓名"
					onChange={(event) => this.handleDelete(event.nativeEvent.text)}
				/>
				<Button 
					title="删除数据"
					onPress={this.handleDelete}
				/>
				<View style={{height: 20}}></View>
				<Button 
					title="删除数据库"
					onPress={this.handleDeleteTable}
				/>
				<View style={{marginTop: 20, marginBottom: 20,}}>
					<Text>修改数据的操作</Text>
					<TextInput 
						placeholder="请输入姓名"
						onChange={(event) => this.setState({name: event.nativeEvent.text})}
					/>
					<TextInput 
						placeholder="请输入年龄"
						onChange={(event) => this.setState({age: event.nativeEvent.text})}
					/>
					<Button 
						title="点击修改"
						onPress={() => this.handleUpdate(this.state.name, this.state.age)}
					/>
				</View>
				<Button 
					title="关闭数据库"
					onPress={this.closeDB}
				/>
			</View>
		);
	}
}